<?php
/**新闻信息操作文件**/
require_once 'common.php';
//添加新闻
function addNews($title,$content,$uid,$classid){
	$link=get_connect();
	$title=mysql_dataCheck($title);
	$content=mysql_dataCheck($content);	
	$format="%Y-%m-%d %H:%M:%S";//设置时间格式
	$publishtime=strftime($format); //获取系统时间	
	$sql = "insert into `tbl_news` (`title`,`content`,`uid`,`classid`,`publishtime`) values ('$title','$content',$uid,$classid,'$publishtime')";			
	$rs= execUpdate($sql,$link);
	return $rs; 
}
//编辑新闻
function updateNews($newsid,$title,$content,$uid,$classid){
    $link=get_connect();
	$classname=mysql_dataCheck($classname);
	$classdesc=mysql_dataCheck($classdesc);	 
	$sql = "update `tbl_newsclass` set `title`='$title',`content`='$content',`uid`=$uid,`classid`=$classid where `newsid`=$newsid";	
    $rs=execUpdate($sql,$link);   
    return $rs;
}
//置顶新闻,根据新闻编号置顶新闻
function updateTopNews($newsid){
  $link=get_connect();
  $sql="update `tbl_news` set `istop`=1 where `newsid`=$newsid ";
  $rs=execUpdate($sql,$link);   
  return $rs;	
}
//置热点新闻,根据新闻编号置热点新闻
function updateHotNews($newsid){
  $link=get_connect();
  $sql="update `tbl_news` set `ishot`=1 where `newsid`=$newsid ";
  $rs=execUpdate($sql,$link);   
  return $rs;	
}
//根据新闻编号修改点赞计数
function updateLikeCount($newsid){
  $link=get_connect();
  $sql="update `tbl_news` set `likecount`=`likecount` + 1 where `newsid`=$newsid ";
  $rs=execUpdate($sql,$link);   
  return $rs;	
}
//根据新闻编号修改阅读计数
function updateViewCount($newsid){
  $link=get_connect();
  $sql="update `tbl_news` set `viewcount`=`viewcount`+1 where `newsid`=$newsid ";
  $rs=execUpdate($sql,$link);   
  return $rs;	
}

//删除新闻
function deleteNews($newsid){
     $sql="delete from `tbl_news` where `newsid`=$newsid";
	 $link=get_connect();
     $rs=execUpdate($sql,$link);   
     return $rs;
}

//取消新闻置顶,根据新闻编号取消新闻置顶
function cancelTopNews($newsid){
  $link=get_connect();
  $sql="update `tbl_news` set `istop`=0 where `newsid`=$newsid ";
  $rs=execUpdate($sql,$link);   
  return $rs;	
}

//取消热点新闻,根据新闻编号取消热点新闻
function cancelHotNews($newsid){
  $link=get_connect();
  $sql="update `tbl_news` set `ishot`=0 where `newsid`=$newsid ";
  $rs=execUpdate($sql,$link);   
  return $rs;	
}

//根据用户编号查找新闻
function findNewsByUid($uid){
   $sql = "select * from `tbl_news` where `uid`=$uid";  
   $link=get_connect();
   $rs=execQuery($sql,$link);   
   return $rs;
}

//按照发布时间倒序查询所有新闻信息
function findNews(){
   $sql = "select * from `tbl_news` order by `publishtime` desc ";  
   $link=get_connect();
   $rs=execQuery($sql,$link);
   return $rs;
}

//根据新闻类别显示相应类别新闻
function findNewsByClassid($classid){
   $sql = "select * from `tbl_news` where `classid`=$classid order by `publishtime` desc ";  
   $link=get_connect();
   $rs=execQuery($sql,$link);
   return $rs;
}
//根据编号查找新闻
function findNewsById($newsid){
   $sql = "select * from `tbl_news` where `newsid`=$newsid";  
   $link=get_connect();
   $rs=execQuery($sql,$link);
   if(count($rs)>0){return $rs[0];}
   return $rs;
}
//按照指定字段，指定关键词模糊查询新闻信息，若$search_field没有设置，则默认对新闻标题和内容字段都进行查找
function findNewsByName($keyword,$search_field="all"){
 if($search_field=="all"){	
     $sql = "select * from `tbl_news` where `title` like '%$keyword%' or `content` like '%$keyword%'  order by `publishtime` desc ";
  }else{
     $sql = "select * from `tbl_news` where `$search_field` like '%$keyword%' order by `publishtime` desc "; 
  }  
   $link=get_connect();
   $rs=execQuery($sql,$link);
   return $rs;
}
//显示热点新闻 若缺省参数，则显示所有的热点新闻，否则显示指定条数的热点新闻
function findHotNews($countlimit=0){	
  $sql="select * from `tbl_news` where `ishot`=1 ";
  if($countlimit!=0){
      $sql=$sql." limit $countlimit";
   }
   $link=get_connect();
   $rs=execQuery($sql,$link);
   return $rs;
}
//显示置顶新闻 若缺省参数，则显示所有的推荐新闻，否则显示指定条数的置顶新闻
function findTopNews($countlimit=0){	
  $sql="select * from `tbl_news` where `istop`=1 ";
  if($countlimit!=0){
      $sql=$sql." limit $countlimit";
   }
   $link=get_connect();
   $rs=execQuery($sql,$link);
   return $rs;
}

///////////////////////////////////////
//加入分页后的新闻查询函数
//////////////////////////////////////
/**
  获取全部新闻分页后的最大页码 
* @param int $pagesize 每页显示最大记录数 默认为10条记录
*/
function maxpage_findNews($pagesize=10){
  $link=get_connect();
  $sql="select count(*) as num from `tbl_news` order by `publishtime` desc"; 
  $rs=execQuery($sql,$link);
  $count= $rs[0];   
  //取出查询结果中的num列的值
  $count = $count['num'];
  //取得最大页码值
  $max_page = ceil($count/$pagesize);
  return $max_page; 
}

/**
  分页查询所有新闻信息，按照发布时间倒序 
* @param int $page 当前page值
* @param int $pagesize 每页显示最大记录数 默认为10条记录
*/
function findNews_page($page,$pagesize=10){
  $max_page=maxpage_findNews($pagesize); 
  //20200428 修改
  if($max_page < 1) return null;
  $page= $page>$max_page? $max_page:$page; 
   //拼接查询语句并执行，获取查询数据
  $lim = ($page -1) * $pagesize;
  $sql = "select * from `tbl_news` order by `publishtime` desc limit $lim, $pagesize";  
  $link=get_connect();
  $rs=execQuery($sql,$link);
  return $rs;
}

/**
  获取分类新闻查询分页后的最大页码
* @param int $classid 新闻类别编号 
* @param int $pagesize 每页显示最大记录数 默认为10条记录
*/
function maxpage_findNewsByClassid($classid,$pagesize=10){
  $link=get_connect();
  $sql="select count(*) as num from `tbl_news` where `classid`=$classid order by `publishtime` desc"; 
  $rs=execQuery($sql,$link);
  $count= $rs[0];   
  //取出查询结果中的num列的值
  $count = $count['num'];
  //取得最大页码值
  $max_page = ceil($count/$pagesize);
  return $max_page; 
}

/**
  分页查询选定类别的新闻信息，按照发布时间倒序
* @param int $classid 新闻类别编号  
* @param int $page 当前page值
* @param int $pagesize 每页显示最大记录数 默认为10条记录
*/
function findNewsByClassid_page($classid,$page,$pagesize=10){
  $max_page = maxpage_findNewsByClassid($classid,$pagesize);
   //20200428 修改
  if($max_page < 1) return null;
     
   //拼接查询语句并执行，获取查询数据
   $page= $page>$max_page? $max_page:$page;
   $lim = ($page -1) * $pagesize;  	
   $sql = "select * from `tbl_news` where `classid`=$classid order by `publishtime` desc limit $lim,$pagesize";  
   $link=get_connect();
   $rs=execQuery($sql,$link);
   return $rs;
}

/**
  获取模糊新闻查询分页后的最大页码
* @param string $keyword 查询内容
* @param string $search_field 查询字段 
* @param int $pagesize 每页显示最大记录数 默认为10条记录
*/
function maxpage_findNewsByName($keyword,$search_field="all",$pagesize=10){
  $link=get_connect();
if($search_field=="all"){	
     $sql = "select count(*) as num from `tbl_news` where `title` like '%$keyword%' or `content` like '%$keyword%'  order by `publishtime` desc ";
  }else{
     $sql = "select count(*) as num from `tbl_news` where `$search_field` like '%$keyword%' order by `publishtime` desc "; 
	
  }     
  
  $rs=execQuery($sql,$link);
  $count= $rs[0];   
  //取出查询结果中的num列的值
  $count = $count['num'];
  //取得最大页码值
  $max_page = ceil($count/$pagesize);
  return $max_page; 
}
/**
  分页查询选定模糊查询的新闻信息，按照发布时间倒序
* @param string $keyword 查询内容
* @param string $search_field 查询字号  
* @param int $page 当前page值
* @param int $pagesize 每页显示最大记录数 默认为10条记录
*/
function findNewsByName_page($keyword,$page,$search_field = "all",$pagesize = 10){
  //取得最大页码值
  $max_page = maxpage_findNewsByName($keyword,$search_field,$pagesize); 
  //20200428 修改
  if($max_page < 1) return null;
  $page= $page>$max_page? $max_page:$page; 
   //拼接查询语句并执行，获取查询数据
  $lim = ($page -1) * $pagesize;
  if($search_field=="all"){	
      $sql = "select * from `tbl_news` where `title` like '%$keyword%' or `content` like '%$keyword%'  order by `publishtime` desc limit $lim, $pagesize";
  }else{
     $sql = "select * from `tbl_news` where `$search_field` like '%$keyword%' order by `publishtime` desc limit $lim,$pagesize"; 
  } 
 
  
   $link=get_connect(); 
   $rs=execQuery($sql,$link);
   return $rs;
}
?>